<?php

namespace  App\Dao\Mongo;

use App\Models\Mongo\Server\File as FileMongo;
// use App\Models\Mongo\Server\FileCommon as FileMongo;
use MongoDB\BSON\ObjectId;
class FileSlice extends BaseMongo
{

    public static function create(string $admins_id, int $is_common)
    {
        //0代表非公共 1代表公共
        return $is_common === 0 ? FileMongo::create(substr($admins_id, 0, 1)) : FileMongo::create();
    }

     public static function insertOne(int $admins_id, int $is_common, int $dir_id, int $slice_size ,array $file_list)
     {
          $create_time  = time();
          return FileMongo::create($admins_id)->insertMany(array_map(function ($item) use ($is_common,$slice_size,$dir_id, $create_time) {
               $arr = [];
               $arr['is_common'] = $is_common;
               $arr['dir_id'] = $dir_id;
               $arr['create_time'] = $create_time;
               $arr['file_name'] = $item['file_name'];
               $arr['file_type'] = $item['file_type'];
               $arr['size'] = $item['size'];
               $arr['slice_size'] = $slice_size;
               $arr['is_complete'] = 0;          //是否上传完成
               $arr['slice_num'] = ceil($item['size'] / 1024 / $slice_size);  //切片次数
               return $arr;
          }, $file_list));
     }

     public static function insert(int $admins_id,int $is_common,array $params){
        $create_time  = time();

        if($is_common){

        }else{
            return FileMongo::create($admins_id, $is_common)->insertOne(
                [
                    'dir_id'    =>    $params['dir_id'],       //文件夹id
                    'file_size' =>    $params['file_size'],    //文件大小
                    'admins_id' =>    $params['admins_id'],
                    'file_url'  =>    $params['file_url'],
                    'file_type' =>    $params['file_type'],
                    'file_name' =>    $params['file_name'],
                    'create_time' =>  $create_time
                ]
            );
        }


     }



    //  public static function find(int $admins_id,string $oid){
    //    return  FileSliceMongo::create($admins_id)->findOne([ '_id' => new ObjectId($oid),'is_complete'=>0]);
    //  }
}
